﻿using QiuShiBaiKe.Commons;
using QiuShiBaiKe.DAL;
using QiuShiBaiKe.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QiuShiBaiKe.BLL
{
    public class UserBLL
    {
        private QiuShiBaiKe.Model.QiuShiBaiKeDBEntities entity;
        private UserDAL userDal;
        public UserBLL(QiuShiBaiKe.Model.QiuShiBaiKeDBEntities entity)
        {
            this.entity = entity;
            userDal = new UserDAL(entity);
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns>新增结果</returns>
        public AddUserResult Add(string userName, string password)
        {
            //检查用户名中是否包含禁用词
            string[] badWords = { "打倒", "管理员", "毛泽东", "毛主席", "admin" };
            bool containsBadWords = false;
            foreach (string badWord in badWords)
            {
                if (userName.Contains(badWord))
                {
                    containsBadWords = true;
                    break;
                }
            }
            if (containsBadWords)
            {
                return AddUserResult.UserNameBadWords;
            }

            //检查用户名长度
            if (userName.Length < 3||userName.Length>15)
            {
                return AddUserResult.UserNameLengthInvalid;
            }

            String passwordHash = CommonHelper.CalcMD5(password);
            //注册，并且检查用户名是是否存在
            bool isOK = userDal.Insert(userName, passwordHash);
            return isOK ? AddUserResult.OK : AddUserResult.UserNameExists;
        }

        /// <summary>
        /// 用户名是否存在
        /// </summary>
        /// <param name="userName">要找的用户名</param>
        /// <returns></returns>
        public bool IsExists(string userName)
        {
            User user = userDal.GetByUserName(userName);
            return user != null;
        }

        /// <summary>
        /// 根据用户Id得到用户的用户名
        /// </summary>
        /// <param name="id">用户Id</param>
        /// <returns>用户名</returns>
        public string GetUserName(long id)
        {
            User user = userDal.GetById(id);
            if (user == null)
            {
                throw new Exception("没有id="+id+"的User");
            }
            return user.UserName;
        }

        /// <summary>
        /// 检查用户名、密码是否正确
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码，未散列</param>
        /// <returns></returns>
        public bool CheckLogin(string username, string password)
        {
            User user = userDal.GetByUserName(username);
            if (user == null)
            {
                return false;
            }
            string passwordHash = CommonHelper.CalcMD5(password);
            return passwordHash == user.PasswordHash;
        }

        public User GetByUserName(string userName)
        {
            return userDal.GetByUserName(userName);
        }
    }

    public enum AddUserResult 
    { 
        OK,
        UserNameExists,//用户名已经存在
        UserNameBadWords,//用户名中含有禁用词
        UserNameLengthInvalid//用户名长度不符合要求
    }
}
